package com.hongdee.market.month.service.impl;

import com.hongdee.common.dao.IHibernateDao;

import com.hongdee.market.month.dao.QZDao;
import com.hongdee.market.month.entity.TMonthlyQz;
import com.hongdee.market.month.service.TQZService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
/**
 * Created by dell on 2017/6/1.
 */
public class TQZServiceImpl implements TQZService {
    @Autowired
    private QZDao qzDao;
    private String tabName = "t_monthly_qz";
    @Autowired
    private IHibernateDao iHibernateDao;

    /**
     * @param \TMonthlyQz 实体
     * @return TMonthlyQz
     * @throws Exception
     * @paramuid 登录用户 ID
     * @desc 新增与更改 保存数据
     * @author 于扬
     */




    public TMonthlyQz saveOrUpdate(TMonthlyQz tMonthlyQz) throws Exception {

        if(!StringUtils.isEmpty(tMonthlyQz.getId())){ // 编辑操作
            	            Date date = new Date();
            	            SimpleDateFormat dfY = new SimpleDateFormat("YYYY");
            	            //SimpleDateFormat dfM = new SimpleDateFormat("MM");
            	            String nowYear = dfY.format(date);
            	            //String nowMonth = dfM.format(date);
            	            int res = nowYear.compareTo(tMonthlyQz.getYear().toString()); // 与当前年对比
            	            if(res>0) { // 当前年份大于编辑年份 => 不允许编辑
                	                throw new Exception("当前年份不允许修改！");
                	            }
            	            else{ // 当前年份小于或等于编辑年份 => 允许编辑
                	                List list= this.qzDao.list("select * from t_monthly_qz where year = ? and id<>? ", tMonthlyQz.getYear(),tMonthlyQz.getId());
                	                if (list.size()>0) {
                    	                    throw new Exception(tMonthlyQz.getYear()+"年权重已存在！");
                    	                }
                	                if ((tMonthlyQz.getJanuary() + tMonthlyQz.getAugust()+ tMonthlyQz.getApril()+ tMonthlyQz.getJuly()+ tMonthlyQz.getDecember()+ tMonthlyQz.getFebruary()+ tMonthlyQz.getJune()+ tMonthlyQz.getMarch()+ tMonthlyQz.getMay()+ tMonthlyQz.getNovember()+ tMonthlyQz.getOctober()+ tMonthlyQz.getSeptember())==100){
                    	                    if (tMonthlyQz.getAugust()==0||tMonthlyQz.getAugust()==100|| tMonthlyQz.getJanuary()==0||tMonthlyQz.getJanuary()==100|| tMonthlyQz.getApril()==0||tMonthlyQz.getApril()==100|| tMonthlyQz.getJuly()==0||tMonthlyQz.getJuly()==100|| tMonthlyQz.getDecember()==0||tMonthlyQz.getDecember()==100|| tMonthlyQz.getFebruary()==0||tMonthlyQz.getFebruary()==100|| tMonthlyQz.getJune()==0||tMonthlyQz.getJune()==100|| tMonthlyQz.getMay()==0||tMonthlyQz.getMay()==100|| tMonthlyQz.getMarch()==0||tMonthlyQz.getMarch()==100|| tMonthlyQz.getNovember()==0||tMonthlyQz.getNovember()==100|| tMonthlyQz.getOctober()==0||tMonthlyQz.getOctober()==100|| tMonthlyQz.getSeptember()==0||tMonthlyQz.getSeptember()==100){
                        	                        throw new Exception("每个月权重值需大于0，小于100");
                        	                    }else {
                        	                            this.qzDao.saveOrUpdate(tMonthlyQz);
                        	                    }
                    	                }else {
                    	                    throw new Exception("十二个月的总和必须等于100");
                    	                }
                            }
            	        }
        	        else { // 新增操作
            	            List list= this.qzDao.list("select * from t_monthly_qz where year = ? ", tMonthlyQz.getYear());
            	            if (list.size()>0) {
                	                //先清空该年度数据(目标管理,目标跟踪),再进行保存
                	            /*String sqlFollow = "DELETE FROM t_crm_month_target_follow WHERE forecast_year = '"+tMonthlyQz.getYear()+"'";
                         String sqlManage = "DELETE FROM t_crm_month_target_manage WHERE year = '"+tMonthlyQz.getYear()+"'";
                        this.qzDao.executeSQL(sqlFollow);
                        this.qzDao.executeSQL(sqlManage);*/
                	            throw new Exception(tMonthlyQz.getYear()+"年权重已存在！");
                            }
            if ((tMonthlyQz.getJanuary() + tMonthlyQz.getAugust()+ tMonthlyQz.getApril()+ tMonthlyQz.getJuly()+ tMonthlyQz.getDecember()+ tMonthlyQz.getFebruary()+ tMonthlyQz.getJune()+ tMonthlyQz.getMarch()+ tMonthlyQz.getMay()+ tMonthlyQz.getNovember()+ tMonthlyQz.getOctober()+ tMonthlyQz.getSeptember())==100){
               if (tMonthlyQz.getAugust()==0||tMonthlyQz.getAugust()==100|| tMonthlyQz.getJanuary()==0||tMonthlyQz.getJanuary()==100|| tMonthlyQz.getApril()==0||tMonthlyQz.getApril()==100|| tMonthlyQz.getJuly()==0||tMonthlyQz.getJuly()==100|| tMonthlyQz.getDecember()==0||tMonthlyQz.getDecember()==100|| tMonthlyQz.getFebruary()==0||tMonthlyQz.getFebruary()==100|| tMonthlyQz.getJune()==0||tMonthlyQz.getJune()==100|| tMonthlyQz.getMay()==0||tMonthlyQz.getMay()==100|| tMonthlyQz.getMarch()==0||tMonthlyQz.getMarch()==100|| tMonthlyQz.getNovember()==0||tMonthlyQz.getNovember()==100|| tMonthlyQz.getOctober()==0||tMonthlyQz.getOctober()==100|| tMonthlyQz.getSeptember()==0||tMonthlyQz.getSeptember()==100){
                     throw new Exception("每个月权重值需大于0，小于100");
                     }else {
                    this.qzDao.saveOrUpdate(tMonthlyQz);
                    }
               }else {
                throw new Exception("十二个月的总和必须等于100");
                }
        }
       return tMonthlyQz;
    }
    /**
     * @param start
     * @param limit
     * @return tCrmAverageEntity
     * @throws Exception
     * @paramuid 登录用户 ID
     * @desc 分页查询
     * @author 于扬
     */

    public Map newPage(String start, String limit, String year) throws Exception {
        String sql = "select * from  t_monthly_qz where 1=1 ";
        List<Object> params = new ArrayList();
        if (!StringUtils.isEmpty(year)) {
            sql += "and year= ?";
            params.add(year);
        }
        return this.qzDao.page(sql, params.toArray(), start, limit);
    }

    /**
     * 详情查询
     * @param id
     * @return
     * @throws Exception
     */
    public Map info(String id) throws Exception {
        String sql = "select * from " + tabName + " where id = ? ";
        Map map = this.qzDao.info(sql, id);
        SimpleDateFormat dfY = new SimpleDateFormat("YYYY");
         SimpleDateFormat dfM = new SimpleDateFormat("M");
        String sqlY = map.get("year").toString();
        int res = sqlY.compareTo(dfY.format(new Date()));
        if(res<0){ // 编辑年小于当前年
             map.put("disabled","all");
             }
        else if(res == 0){ // 编辑年等于当前年
         map.put("disabled",dfM.format(new Date()));
          }
         else {
             map.put("disabled","-1");
            }
        return map;
    }

}
